package com.naiterui.ehp.bs.ds.common.config;

import feign.RequestInterceptor;
import java.net.URLEncoder;

import org.apache.commons.codec.Charsets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.security.HeaderConstant;
import com.naiterui.ehp.bp.support.security.InterceptorAddition;
import com.naiterui.ehp.bs.ds.common.interceptor.PasswordExpireInterceptor;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class AppConfiguration {

    //@Bean
    public InterceptorAddition interceptorAddition() {
        return InterceptorAddition.build(new PasswordExpireInterceptor()).excludePathPatterns("/sys/menu/nav", "/sys/user/info", "/sys/user/password");
    }

    /**
     * 由于使用eureka+feign，所以请求的时候使用的是IP:PORT请求，所以需要在header中增加X-inner
     *
     * @return /
     */
    @Bean
    public RequestInterceptor feignInterceptor() {
        return template -> {
            template.header("X-inner", "1");
            template.header(HeaderConstant.DATA_NO_WRAP, "1");
            try {
                template.header("operator", URLEncoder.encode(SecurityUtils.getCurrentUser().getUsername(), Charsets.UTF_8.name()));
            } catch (Exception ignored) {
                template.header("operator", "ds");
            }
        };
    }

}
